package cn.fancy.sdk.pay;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.fancy.sdk.baseview.DialogView;
import cn.fancy.sdk.common.utils.ResUtil;
import cn.fancy.sdk.entity.DevBase.DevEnv;
import cn.fancy.sdk.pay.ali.AlipayH5Activity;
import cn.fancy.sdk.pay.ali.AlipayH5Activity.JSInterface;

public class WebViewActivity extends Activity implements OnClickListener{
	private static String URL="url";
	private static String TITLE="title";
	private LinearLayout guildlogobarLl;
    /**
	 * 标题
	 */
	private TextView titleTv;
	
	/**
	 * 返回键
	 */
	private ImageView backIv;
	private WebView webView;
	
	private String url;
	private Dialog dialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(ResUtil.getLayoutId(WebViewActivity.this, "guild_pay_web_activity"));
        guildlogobarLl = (LinearLayout) this.findViewById(ResUtil.getId(WebViewActivity.this, "guild_logo_bar"));
        titleTv = (TextView) this.findViewById(ResUtil.getId(WebViewActivity.this, "guild_pay_title"));
        backIv = (ImageView) this.findViewById(ResUtil.getId(WebViewActivity.this, "guild_pay_back"));
        if (DevEnv.LOGO == 0) {
            guildlogobarLl.setVisibility(View.GONE);
        }
        
        backIv.setOnClickListener(this);
        webView = (WebView) this.findViewById(ResUtil.getId(WebViewActivity.this, "guild_pay_wv"));
        Intent intent = this.getIntent();
        url = intent.getStringExtra(URL);
        titleTv.setText(intent.getStringExtra(TITLE));
        initView();
        dialog = DialogView.showWaitingDialog(WebViewActivity.this, new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                
                WebViewActivity.this.finish();
            }
        });
	}
    
	private void initView() {
		webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		webView.getSettings().setAppCacheEnabled(false);
		webView.getSettings().setDatabaseEnabled(false);
		webView.getSettings().setDomStorageEnabled(false);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webView.getSettings().setDefaultTextEncodingName("UTF-8");
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl(url);
        webView.addJavascriptInterface(new JSInterface(), "JSInterface");
        //webView.addJavascriptInterface(new PayCbJsInterface(WebViewActivity.this, handler, what),"FancySdk");
        
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
                DialogView.cancelDialog(dialog);
                webView.loadUrl("javascript:function finishActivity(){JSInterface.finishActivity();}");
				webView.loadUrl("javascript:function closeFloat(){JSInterface.closeFloat();}");
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		this.finish();
	}
    
    public class JSInterface{
        @android.webkit.JavascriptInterface
        public void finishActivity(){
            WebViewActivity.this.finish();
        }
	
		@android.webkit.JavascriptInterface
		public void closeFloat(){
			WebViewActivity.this.finish();
		}
    }
  
    public static void start(Context context, String url, String title){
    	Intent intent = new Intent();
		intent.putExtra(URL, url);
		intent.putExtra(TITLE, title);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setClass(context, WebViewActivity.class);
		context.startActivity(intent);
    }

	@Override
	public void onClick(View v) {
		if (v.getId() == ResUtil.getId(WebViewActivity.this, "guild_pay_back")) {
			this.onBackPressed();
		}
	}
}
